package crivia.ecp.common;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import weaver.conn.RecordSetTrans;
import weaver.general.SecurityHelper;
import weaver.servicefiles.DataSourceXML;
import crivia.db.exception.LostCammand;
import crivia.db.i.CMD;
import crivia.db.i.Specially;
import crivia.db.jdbc.CMD_jdbc;
import crivia.db.jdbc.DB;
import crivia.eca.a.EcologyCMD;
import crivia.eca.a.EcologyCMD_Trans;
import crivia.txt.common.PropertiesIO;

public class EcologyDB {
	
	private static final String ECOLOGYDBVMK="ECOLOGY-DBVM";
	private static Map<String,String> DBVM=new HashMap<String, String>();
	
	public static String relationHrmResource(String ids){
		return relationHrmResource(ids , null);
	}
	public static String relationHrmResource(String ids , CMD cmd){
		return crivia.db.common.SQL.relation(ids, "HrmResource", "lastName", "id" 
			,cmd == null ? db() : cmd);
	}
	
	public static String relationDepartment(String ids){
		return relationDepartment(ids, null);
	}
	public static String relationDepartment(String ids , CMD cmd){
		return crivia.db.common.SQL.relation(ids, "HrmDepartment", "DepartmentName", "id"
			,cmd == null ? db() : cmd);
	}
	
	public static String relationRequest(String ids){
		return relationRequest(ids, null);
	}
	public static String relationRequest(String ids , CMD cmd){
		return crivia.db.common.SQL.relation(ids, "workflow_requestBase", "requestName", "requestID"
			,cmd == null ? db() : cmd);
	}
	
	public static CMD_jdbc jdbc(){
		String f = "weaver.properties";
		String dk = "DriverClasses";
		String d = EPM.get(f, dk, null);
		if (!ECR.hasCondition(d)){
			PropertiesIO.cleanProperties(EPM.toPropertiesPath(f));
			d = EPM.get(f, dk, null);
		}
		return new CMD_jdbc(new DB(d
				, EPM.get(f, "ecology.url", null)
				, EPM.get(f, "ecology.user", null)
				, EPM.get(f, "ecology.password", null)
		));
	}
	
	public static CMD_jdbc jdbc(String dataSourceName){
		@SuppressWarnings("rawtypes")
		Hashtable dataSource = (Hashtable) new DataSourceXML()
			.getDataHST().get(dataSourceName);
		if (null == dataSource){
			throw new LostCammand("Data Source ["+dataSourceName+"] Not Found.");
		}
		try {
			String url="";
			if (dataSource.containsKey("url"))url=String.valueOf(dataSource.get("url"));
			if (String.valueOf(dataSource.get("type")).indexOf(crivia.db.common.SQL.DataBaseType_Oracle) > -1){
				if(ECR.hasCondition(url))
					return new CMD_jdbc(new DB(
							DB.Driver.Oracle , url
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("user")))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("password")))
					));
				else
					return new CMD_jdbc(new DB(
							DB.Driver.Oracle
							, String.valueOf(dataSource.get("host")+":"+dataSource.get("port"))
							, String.valueOf(dataSource.get("dbname"))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("user")))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("password")))
					));
			} else if (String.valueOf(dataSource.get("type")).indexOf(crivia.db.common.SQL.DataBaseType_SQL_Server) > -1) {
				if(ECR.hasCondition(url))
					return new CMD_jdbc(new DB(
							DB.Driver.SQLServer , url
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("user")))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("password")))
					));
				else
					return new CMD_jdbc(new DB(
							DB.Driver.SQLServer
							, String.valueOf(dataSource.get("host")+":"+dataSource.get("port"))
							, String.valueOf(dataSource.get("dbname"))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("user")))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("password")))
					));
			} else if (String.valueOf(dataSource.get("type")).indexOf(crivia.db.common.SQL.DataBaseType_MySQL) > -1) {
				if(ECR.hasCondition(url))
					return new CMD_jdbc(new DB(
							DB.Driver.MySQL , url
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("user")))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("password")))
					));
				else
					return new CMD_jdbc(new DB(
							DB.Driver.MySQL
							, String.valueOf(dataSource.get("host")+":"+dataSource.get("port"))
							, String.valueOf(dataSource.get("dbname"))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("user")))
							, SecurityHelper.decrypt("ecology",String.valueOf( dataSource.get("password")))
					));
			} else {
				throw new LostCammand("Unknow Data Base Type.");
			}
		} catch (Exception e) {
			throw new LostCammand(e);
		}
	}
	
	public static CMD_jdbc jdbcMaybeName(String s){
		if(ECR.hasCondition(s))return jdbc(s);else return jdbc();
	}
	
	public static EcologyCMD db(){
		String dbv=DBVM.get(ECOLOGYDBVMK);
		if(ECR.hasCondition(dbv))
			return new EcologyCMD(null,null,dbv);
		else{
			EcologyCMD db=new EcologyCMD();
			dbv=db.DataBaseVersion();
			DBVM.put(ECOLOGYDBVMK, dbv);
			return db;
		}
	}
	
	public static EcologyCMD db(Specially specially){
		String dbv=DBVM.get(ECOLOGYDBVMK);
		if(ECR.hasCondition(dbv))
			return new EcologyCMD(null,specially,dbv);
		else{
			EcologyCMD db=new EcologyCMD(specially);
			dbv=db.DataBaseVersion();
			DBVM.put(ECOLOGYDBVMK, dbv);
			return db;
		}
	}
	
	public static EcologyCMD db(String dataSourceName){
		return db(dataSourceName, null);
	}
	
	public static EcologyCMD db(String dataSourceName , Specially specially){
		String dbv=DBVM.get(dataSourceName);
		if(ECR.hasCondition(dbv))
			return new EcologyCMD(dataSourceName,specially,dbv);
		else{
			EcologyCMD db=new EcologyCMD(dataSourceName,specially);
			dbv=db.DataBaseVersion();
			DBVM.put(dataSourceName,dbv);
			return db;
		}
	}
	
	public static EcologyCMD dbMaybeName(String s){
		if(ECR.hasCondition(s))return db(s);else return db();
	}
	
	public static EcologyCMD_Trans dbTrans(){
		return new EcologyCMD_Trans();
	}
	
	public static EcologyCMD_Trans dbTrans(RecordSetTrans db){
		return new EcologyCMD_Trans(db);
	}
	
	public static class specially {
		public static Specially oracle = EcologyCMD.oracle;
		public static Specially sqlserver = EcologyCMD.sqlserver;
	}
	
//	public static void main(String[] args) {
//		EcologyDB.db("k3", EcologyDB.specially.sqlserver);
//	}

}
